package com.example.xujin.owncode;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.xujin.owncode.Bean.ClassInfoBean;
import com.example.xujin.owncode.Bean.NumBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xujin on 2019/7/15.
 */

public class Activity_2 extends AppCompatActivity {
    private List<ClassInfoBean.DataBean> list = new ArrayList<>();
    private List<Double> doubleList = new ArrayList<>();
    NumBean numBean;
    private List<NumBean> numList = new ArrayList<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_02);
        initData();
        initView();
    }

    private void initView() {
    }

    private void initData() {  ///api/v1/course/table/queryTable.ns
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder formBody = new FormBody.Builder();
        formBody.add("courseId", "20181129005");
        Request request = new Request.Builder()
                .header("content-type", "application/json")
                .header("token", "B9qBF2zNb2lZ0Vr05pZXn2KZyZyO8jeHc79OupOXPReXEh+/C56JBvoDBXizImtT")
                .header("Platform", "1")
                .url("http://www.e5-dance.com:8080/api/v1/course/table/queryTable.ns")
                .post(formBody.build())
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                ClassInfoBean bean = gson.fromJson(data, ClassInfoBean.class);
                list.addAll(bean.getData());
                //得到不重复的月份数据  dataList
                List<String> dataList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    dataList.add(list.get(i).getHowDate().substring(0, 7));
                }
                for (int i = 0; i < dataList.size(); i++) {
                    for (int k = dataList.size() - 1; k > i; k--) {
                        if (dataList.get(k).equals(dataList.get(i))) {
                            dataList.remove(k);
                        }
                    }
                }
                //得到{month:xx,num:xx} 的数据
                for (int i = 0; i < dataList.size(); i++) {
                    for (int j = 0; j < list.size(); j++) {
                        numBean = new NumBean();
                        if (dataList.get(i).equals(list.get(j).getHowDate().substring(0, 7))) ;
                        numBean.setMonth(list.get(j).getHowDate().substring(0, 7));
                        numBean.setNum(list.get(j).getClassFire());
                        numList.add(numBean);
                    }
                }
                for (int i = 0; i < numList.size(); i++) {
                    for (int j = numList.size() - 1; j > i; j--) {

                    }
                }
                //得到同一月份的课消数据
                for (int i = 0; i < numList.size(); i++) {
                    for (int j = numList.size() - 1; j > i; j--) {

                    }
                }
                List<NumBean> list02 = getNewList(numList);
                Log.i("xuwudia", "dataList====" + dataList.toString());
                Log.i("xuwudia", "List=====" + list.toString());
                Log.i("xuwudia", "当前的数据====" + numList.toString());
                Log.i("xuwudia", "list02=====" + list02.toString());

                handler.sendEmptyMessage(1);
            }
        });
    }

    public static List<NumBean> getNewList(List<NumBean> oldList) {
        HashMap<String, NumBean> tempMap = new HashMap<String, NumBean>();

        // 去掉重复的key
        for (NumBean person : oldList) {
            String name = person.getMonth();
            // containsKey(Object key)该方法判断Map集合中是否包含指定的键名，如果包含返回true，不包含返回false
            // containsValue(Object
            // value)该方法判断Map集合中是否包含指定的键值，如果包含返回true，不包含返回false
            if (tempMap.containsKey(name)) {
                NumBean newPerson = new NumBean();
                newPerson.setMonth(name);
                // 合并相同key的value
                newPerson.setNum(tempMap.get(name).getNum() + person.getNum());
                // HashMap不允许key重复，当有key重复时，前面key对应的value值会被覆盖
                tempMap.put(name, newPerson);
            } else {
                tempMap.put(name, person);
            }
        }
        List<NumBean> newList = new ArrayList<NumBean>();
        Iterator iter = tempMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            NumBean newPerson = (NumBean) entry.getValue();
            newList.add(newPerson);
        }
        return newList;
    }


}
